#include <node.h>

namespace {

void GetDetailsCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  // 在这里添加你的 C 代码逻辑，实现 loadTimes 方法的功能
  printf("C code: loadTimes method called.\n");
}

void GetIsInstalledCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  // 在这里添加你的 C 代码逻辑，实现 loadTimes 方法的功能
  printf("C code: loadTimes method called.\n");
}
void InstallStateCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  // 在这里添加你的 C 代码逻辑，实现 loadTimes 方法的功能
  printf("C code: loadTimes method called.\n");
}
void RunningStateCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  // 在这里添加你的 C 代码逻辑，实现 loadTimes 方法的功能
  printf("C code: loadTimes method called.\n");
}

void LoadTimesCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  // 在这里添加你的 C 代码逻辑，实现 loadTimes 方法的功能
  printf("C code: loadTimes method called.\n");
}

void Initialize(v8::Local<v8::Object> exports) {
  v8::Isolate* isolate = exports->GetIsolate();

    // 创建一个包含 GetDetails 方法的 JavaScript 函数
    v8::Local<v8::FunctionTemplate> getDetailsFunctionTemplate = v8::FunctionTemplate::New(isolate, GetDetailsCallback);
    v8::Local<v8::Function> getDetailsFunction = getDetailsFunctionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();

    // 创建一个包含 getIsInstalled 方法的 JavaScript 函数
    v8::Local<v8::FunctionTemplate> getIsInstalledFunctionTemplate = v8::FunctionTemplate::New(isolate, GetIsInstalledCallback);
    v8::Local<v8::Function> getIsInstalledFunction = getIsInstalledFunctionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();

    // 创建一个包含 InstallState 方法的 JavaScript 函数
    v8::Local<v8::FunctionTemplate> installStateFunctionTemplate = v8::FunctionTemplate::New(isolate, InstallStateCallback);
    v8::Local<v8::Function> installStateFunction = installStateFunctionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();

    // 创建一个包含 runningState 方法的 JavaScript 函数
    v8::Local<v8::FunctionTemplate> runningStateFunctionTemplate = v8::FunctionTemplate::New(isolate, RunningStateCallback);
    v8::Local<v8::Function> runningStateFunction = runningStateFunctionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();

//     Nan::HandleScope scope;
// //     // 创建一个字典对象
//       v8::Local<v8::Object> chromeAppObject = Nan::New<v8::Object>();
//
//   // 设置字典的属性
//     appDictionary->Set(Nan::New("isInstalled").ToLocalChecked(), Nan::New(false));


    // 创建 chrome.app 对象
    v8::Local<v8::Object> chromeAppObject = v8::Object::New(isolate);
    // 设置字典的属性
//     chromeAppObject->Set(Nan::New("isInstalled").ToLocalChecked(), Nan::New(false));
    chromeAppObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "getDetails").ToLocalChecked(), getDetailsFunction).FromJust();
    chromeAppObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "getIsInstalled").ToLocalChecked(), getIsInstalledFunction).FromJust();
    chromeAppObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "installState").ToLocalChecked(), installStateFunction).FromJust();
    chromeAppObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "runningState").ToLocalChecked(), runningStateFunction).FromJust();




  // 创建一个包含 loadTimes 方法的 JavaScript 函数
  v8::Local<v8::FunctionTemplate> loadTimesFunctionTemplate = v8::FunctionTemplate::New(isolate, LoadTimesCallback);
  v8::Local<v8::Function> loadTimesFunction = loadTimesFunctionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();

  // 创建 chrome 对象
  v8::Local<v8::Object> chromeObject = v8::Object::New(isolate);

  // 向 chrome 对象添加 loadTimes 方法
  chromeObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "loadTimes").ToLocalChecked(), loadTimesFunction).FromJust();
  chromeObject->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "app").ToLocalChecked(), chromeAppObject).FromJust();

  // 将 chrome 对象导出到 Node.js
  exports->Set(isolate->GetCurrentContext(), v8::String::NewFromUtf8(isolate, "chrome").ToLocalChecked(), chromeObject).FromJust();
}

}  // namespace


NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
